package com.tbit.api;

import com.alibaba.fastjson.JSON;
import com.tbit.manager.api.QuickReplyApi;
import com.tbit.manager.api.dto.QuickReplyDTO;
import com.tbit.uqbike.object.pojo.QuickReply;
import com.tbit.uqbike.webmanager.dao.core.QuickReplyDao;
import com.tbit.uqbike.webmanager.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@RestController
public class QuickReplyApiImpl implements QuickReplyApi {
    @Resource
    private QuickReplyDao quickReplyDao;

    /**
     * @description: 查询快捷回复列表数据
     * @author: mo.shanyong
     * @date: 2025/2/11 15:19
     * @param: quickReplyDTO 查询条件
     * @param: pageNum 页码
     * @param: pageSize 页长
     * @param: search 搜索内容
     * @return: com.tbit.manager.api.view.JsonResult
     */
    @Override
    public Map<String, Object> queryByPage(String quickReplyDTOStr) {
        QuickReplyDTO quickReplyDTO = JSON.parseObject(quickReplyDTOStr, QuickReplyDTO.class);
        Map<String, Object> map = new HashMap<>();
        QuickReply quickReply = new QuickReply();
        quickReply.setUserId(quickReplyDTO.getUserId());
        quickReply.setAccountId(quickReply.getAccountId());
        quickReply.setPlatform(quickReplyDTO.getPlatform());
        log.info("queryByPage---->quickReply:{}",JSON.toJSONString(quickReply));
        List<QuickReply> quickReplyList = this.quickReplyDao.queryAllByLimit(quickReply,
                Objects.nonNull(quickReplyDTO.getPageNum()) && Objects.nonNull(quickReplyDTO.getPageSize()) ? (quickReplyDTO.getPageNum() - 1) * quickReplyDTO.getPageSize() : null,
                quickReplyDTO.getPageSize(), quickReplyDTO.getSearch());
        Integer count = quickReplyDao.count(quickReply, quickReplyDTO.getSearch());
        map.put("data",quickReplyList);
        map.put("total",count);
        return map;
    }

    /**
     * @description: 新增快捷回复
     * @author: mo.shanyong
     * @date: 2025/2/11 15:34
     * @param: quickReplyDTO 新增数据
     * @return: com.tbit.manager.api.view.JsonResult
     */
    @Override
    public void add(QuickReplyDTO quickReplyDTO) {
        QuickReply quickReply = BeanUtils.copyProperties(quickReplyDTO, QuickReply.class);
        Date now = new Date();
        quickReply.setUpdateTime(now);
        log.info("add---->quickReply:{}",JSON.toJSONString(quickReply));
        this.quickReplyDao.insertCheckNull(quickReply);
    }

    /**
     * @description: 编辑快捷回复
     * @author: mo.shanyong
     * @date: 2025/2/11 15:40
     * @param: quickReplyDTO 快捷回复修改的内容
     * @return: com.tbit.manager.api.view.JsonResult
     */
    @Override
    public void edit(QuickReplyDTO quickReplyDTO) {
        QuickReply quickReply = BeanUtils.copyProperties(quickReplyDTO, QuickReply.class);
        Date now = new Date();
        quickReply.setUpdateTime(now);
        log.info("edit---->quickReply:{}",JSON.toJSONString(quickReply));
        this.quickReplyDao.updateCheckNull(quickReply);
    }
    /**
     * @description: 删除快捷回复
     * @author: mo.shanyong
     * @date: 2025/2/11 15:42
     * @param: id 快捷回复id
     * @return: com.tbit.manager.api.view.JsonResult
     */
    @Override
    public void deleteById(Integer id) {
        log.info("deleteById---->id:{}",id);
        this.quickReplyDao.deleteById(id);
    }
}
